package com.northming.daytodo.controller.todo;

import java.util.ArrayList;
import java.util.List;




import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.northming.daytodo.conts.SourceConts;
import com.northming.daytodo.conts.StatusConts;
import com.northming.daytodo.conts.ToDoTypesConts;
import com.northming.daytodo.domain.todo.Todo;
import com.northming.daytodo.domain.todo.TodoInfo;
import com.northming.daytodo.domain.todo.TodoStatus;
import com.northming.daytodo.mapper.graph.GraphMapper;
import com.northming.daytodo.mapper.todo.TodoMapper;
import com.northming.daytodo.model.UserInfo;
import com.northming.daytodo.util.TimeUtil;
import com.northming.daytodo.util.UserUtil;


@RestController
@RequestMapping("/todo")
public class ToDoController {
	
	@Autowired
	private TodoMapper todoMapper;
	@Autowired
	private GraphMapper graphMapper;
	
	@Autowired
	private  UserUtil userUtil;
	
	
	
	
	
	@RequestMapping("/add")
	public Object add(TodoInfo todoInfo){
		TodoInfo curTodoInfo = null;
		Integer userId = userUtil.getUserId();
		Todo todo = new Todo();
		BeanUtils.copyProperties(todoInfo, todo);
		todo.setCreateUser(userId);
		todo.setCreateTime(TimeUtil.getNow());
		todo.setStatus(StatusConts.USE);
		TodoStatus todoStatus = new TodoStatus();
		todoStatus.setUserId(userId);
		todoStatus.setStatus(StatusConts.USE);
		if(addTodo(todo,todoStatus)){
			todoInfo.setId(todo.getId());
			curTodoInfo = todoInfo;
		}
		return curTodoInfo;
			
	}
	
	@RequestMapping("/update")
	public Object update(TodoInfo todoInfo){
		TodoInfo curTodoInfo = null;
		Integer userId = userUtil.getUserId();
		Todo todo = new Todo();
		BeanUtils.copyProperties(todoInfo, todo);
		
		if(updateTodo(todo,userId)){
			curTodoInfo = todoMapper.getTodo(todo.getId(), userId, StatusConts.USE);
			processSourceRef(curTodoInfo);
		}
		return curTodoInfo;
			
	}
	


	private boolean updateTodo(Todo todo,Integer userId) {
		return todoMapper.updateTodo(todo,userId) ==1;
	}

	@Transactional
	public boolean addTodo(Todo todo, TodoStatus todoStatus) {
		int insertTodo = todoMapper.insertTodo(todo);
		int insertTodoStatus = 0;
		if(todo.getId()!=null){
			todoStatus.setTodoId(todo.getId());
			insertTodoStatus = todoMapper.insertTodoStatus(todoStatus);
		}
		return insertTodo+insertTodoStatus==2?true:false;
	}
	/**
	 * 
	 * <pre>
	 * 方法说明：获取待办清单
	 * 
	 * 开发时间：2016年11月28日下午10:40:03
	 * 
	 * 开发人员：gl
	 * 
	 * @return
	 * 
	 * </pre>
	 *
	 */
	@RequestMapping("/list")
	public Object lists(
			@RequestParam(required=true)String date
			){
		
		return getToDos(date);
	}
	
	@RequestMapping("/todayList")
	public Object todayLists(
			@RequestParam(required=true)String userId
			){
		return getToDos(TimeUtil.getToday());
	}
	
	
	
	@RequestMapping("/finish")
	public boolean finish(
			@RequestParam(required=true)Integer todoId
			){
		Integer userId = userUtil.getUserId();
		return todoMapper.changeFinish(todoId,userId,true,TimeUtil.getNow())>0;
	}
	@RequestMapping("/unFinish")
	public boolean unFinish(
			@RequestParam(required=true)Integer todoId
			){
		Integer userId = userUtil.getUserId();
		return todoMapper.changeFinish(todoId,userId,false,null)>0;
	}
	/**
	 * delete todo
	 * @param todoId
	 * @return delete status
	 */
	@RequestMapping("/del")
	public boolean del(@RequestParam(required=true)Integer todoId){
		Integer userId = userUtil.getUserId();
		return todoMapper.changeStatus(todoId,userId,StatusConts.DELETE)==1;
	}
	

	private Object getToDos(String date) {
		Integer userId = userUtil.getUserId();
		List<TodoInfo> todoInfos = todoMapper.getTodoInfos(userId, date,StatusConts.USE);
		for (int i = 0; i < todoInfos.size(); i++) {
			processSourceRef(todoInfos.get(i));			
		}
		return todoInfos;
		
		
	}
	/**
	 * process source ref
	 * @param todoInfo
	 */
	private void processSourceRef(TodoInfo todoInfo) {
		Integer sourceRefId = todoInfo.getSourceRefId();
		switch (todoInfo.getSourceId()) {
		case SourceConts.GRAPH:
			todoInfo.setSourceRefId(graphMapper.getGraphItem(sourceRefId).getGraphId());
			break;
		default:
			break;
		}
		
	}
	
	
	
}
